/**
 * 公共axios配置
 * @author nikolatesfei
 * 
 */
import axios from 'axios'
import qs from 'qs'
import Message from 'iview/src/components/message'
import Loading from 'iview/src/components/loading-bar'

Message.config({
    duration: 4
});

//超时时间
axios.defaults.timeout = 5000;
//默认头部类型
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

axios.interceptors.request.use((config) => {
    Loading.start();
	if(config.method  === 'post'){
	    config.data = qs.stringify(config.data);
	}
  	return config;
},(error) =>{
    Message.error(error.message);return false;
});

axios.interceptors.response.use((res) =>{
    Loading.finish();
    var data = res.data;
    if(data.code==-10000){
        window.location.href = "/login";return false;
    }
    if(data.code==1){
        Message.error(data.msg); return false;
    }else if(data.code==0 && data.msg!=''){
        Message.success(data.msg);
    }
    return res;
}, (error) => {
    Message.error(error.message);return false;
});

export default axios